<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv">
              <div slot="right" style="width: 100%" />
              <el-form-item slot="right" label="事件类型" prop="otherEventTypeArr">
                <div style="display: inline-block; width: 100%">
                  <el-checkbox-group v-model="models.otherEventTypeArr" style="display: inline-block">
                    <el-checkbox
                      v-for="(item, index) in otherEventTypeList"
                      :key="index"
                      :label="item.value"
                    >
                      <div style="display: flex;align-items: center;">
                        <div style="display: flex;align-items: center;">
                          {{ item.label }}
                        </div>
                        <!--        二维数据 :              -->
                        <div v-if="models.otherEventTypeArr && models.otherEventTypeArr.indexOf('230') > -1 && item.value === '230'" style="margin-left: 5px;display: flex;align-items: center;">
                          (
                          <el-checkbox-group v-model="models.otherEventType1TypeArr">
                            <el-checkbox v-for="(item2, index) in otherEventType1TypeList" :key="index" :label="item2.value"> {{ item2.label }} </el-checkbox>
                            <el-input
                              v-if="models.otherEventType1TypeArr && models.otherEventType1TypeArr.indexOf('244') > -1"
                              :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                              placeholder="请输入"
                              v-model="models.baqsjRecordAccidentOther.otherEventType1TypeOther"
                            />
                          </el-checkbox-group>
                          )
                        </div>
                        <!--        二维数据 :              -->
                        <div v-if="models.otherEventTypeArr && models.otherEventTypeArr.indexOf('235') > -1 && item.value === '235'" style="margin-left: 5px;display: flex;align-items: center;">
                          (
                          <el-checkbox-group v-model="models.otherEventType2TypeArr">
                            <el-checkbox v-for="(item3, index) in otherEventType2TypeList" :key="index" :label="item3.value"> {{ item3.label }} </el-checkbox>
                            <el-input
                              v-if="models.otherEventType2TypeArr && models.otherEventType2TypeArr.indexOf('253') > -1"
                              :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                              placeholder="请输入"
                              v-model="models.baqsjRecordAccidentOther.otherEventType2TypeOther"
                            />
                          </el-checkbox-group>
                          )
                        </div>
                      </div>
                    </el-checkbox>
                    <el-input
                      v-if="models.otherEventTypeArr && models.otherEventTypeArr.indexOf('236') > -1"
                      :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordAccidentOther.otherEventTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </event-info>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
              <el-form-item slot="right" label="医疗介入" prop="medicalInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.medicalInvolvedArr">
                    <el-checkbox v-for="(item, index) in medicalInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.medicalInvolvedArr && models.medicalInvolvedArr.indexOf('125') > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.medicalInvolvedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div slot="right" style="width: 100%" />
              <el-form-item slot="right"  label="其他介入" prop="otherInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.otherInvolvedArr">
                    <el-checkbox v-for="(item, index) in otherInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.otherInvolvedArr && models.otherInvolvedArr.indexOf('134')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.otherInvolvedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </measure-two-info>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="其他事件详情">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">事件类型</td>
                <td>{{ getOtherEventTypes(models.otherEventTypeArr) || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">事件详情</td>
                <td>{{ getOtherEventDetails(models.otherEventTypeArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
            <tr slot="right">
              <td class="td-title">医疗介入</td>
              <td>{{ formatMedicalInterventions(models.medicalInvolvedArr) || '' }}</td>
            </tr>
            <tr slot="right">
              <td class="td-title">其他介入</td>
              <td>{{ formatOtherInterventions(models.otherInvolvedArr) || '' }}</td>
            </tr>
          </measure-two-info>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveAccidentOther } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureTwoInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureTwoInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'AccidentOther',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureTwoInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {},
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      otherEventTypeList: [
        { value: '230', label: '⼈际冲突' },
        { value: '231', label: '⾝体攻击' },
        { value: '232', label: '⾃杀/⾃杀倾向（可能造成⽣命危害）' },
        { value: '233', label: '破坏设备' },
        { value: '234', label: '⾃伤（不⾜以造成⽣命危害）' },
        { value: '235', label: '病⼈出⾛' },
        { value: '236', label: '其他' }
      ],
      otherEventType1TypeList: [
        { value: '240', label: '病⼈与家属之间' },
        { value: '241', label: '家属/委托⼈与员⼯之间' },
        { value: '242', label: '病⼈与员⼯之间' },
        { value: '243', label: '员⼯之间' },
        { value: '244', label: '其他' }
      ],
      otherEventType2TypeList: [
        { value: '250', label: '病⼈经同意后外出' },
        { value: '251', label: '病⼈未经同意擅⾃外出' },
        { value: '252', label: '员⼯未意识到病⼈外出' },
        { value: '253', label: '其他' }
      ],
      medicalInterventionList: [
        { value: '120', label: '持续观察' },
        { value: '121', label: '加强护理防范' },
        { value: '122', label: '监测生命体征' },
        { value: '123', label: '伤口护理' },
        { value: '125', label: '其他' }
      ],
      otherInterventionList: [
        { value: '130', label: '予以病人家属慰问和支持' },
        { value: '131', label: '移除与危害相关的设备或环境运作' },
        { value: '134', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        otherEventTypeArr: (info.baqsjRecordAccidentOther && info.baqsjRecordAccidentOther.otherEventType) ? info.baqsjRecordAccidentOther.otherEventType.split(',') : [],
        otherEventType1TypeArr: (info.baqsjRecordAccidentOther && info.baqsjRecordAccidentOther.otherEventType1Type) ? info.baqsjRecordAccidentOther.otherEventType1Type.split(',') : [],
        otherEventType2TypeArr: (info.baqsjRecordAccidentOther && info.baqsjRecordAccidentOther.otherEventType2Type) ? info.baqsjRecordAccidentOther.otherEventType2Type.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'AccidentOther',
        eventName: '意外事件-其他意外事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 其他意外信息 => 对象类型(不同的表单，定制不同)
        baqsjRecordAccidentOther: {},
        // 其他意外信息 =>  (不同的表单，定制不同)
        otherEventTypeArr: [],
        otherEventType1TypeArr: [],
        otherEventType2TypeArr: []
      }
      this.dialogVisible = true
    },
    getOtherEventTypes(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.otherEventTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '230') {
            const type1Labels = [];
            this.otherEventType1TypeList.forEach(type1Item => {
              if (this.models.otherEventType1TypeArr && this.models.otherEventType1TypeArr.includes(type1Item.value)) {
                if (type1Item.value === '244') {
                  type1Labels.push(`其他（${this.models.baqsjRecordAccidentOther.otherEventType1TypeOther || ''}）`);
                } else {
                  type1Labels.push(type1Item.label);
                }
              }
            });
            labels.push(`${item.label}（${type1Labels.join(', ')}）`);
          } else if (item.value === '235') {
            const type2Labels = [];
            this.otherEventType2TypeList.forEach(type2Item => {
              if (this.models.otherEventType2TypeArr && this.models.otherEventType2TypeArr.includes(type2Item.value)) {
                if (type2Item.value === '253') {
                  type2Labels.push(`其他（${this.models.baqsjRecordAccidentOther.otherEventType2TypeOther || ''}）`);
                } else {
                  type2Labels.push(type2Item.label);
                }
              }
            });
            labels.push(`${item.label}（${type2Labels.join(', ')}）`);
          } else if (item.value === '236') {
            labels.push(`其他（${this.models.baqsjRecordAccidentOther.otherEventTypeOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getOtherEventDetails(values) {
      if (!values || values.length === 0) return '';
      const details = [];
      values.forEach(value => {
        if (value === '230') {
          details.push('人际冲突');
        } else if (value === '235') {
          details.push('病人出走');
        } else if (value === '236') {
          details.push('其他');
        }
      });
      return details.join(', ');
    },
    formatMedicalInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.medicalInterventionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '125') {
            labels.push(`其他（${this.models.baqsjRecordHandle.medicalInvolvedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    formatOtherInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.otherInterventionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '134') {
            labels.push(`其他（${this.models.baqsjRecordHandle.otherInvolvedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',');
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',');
          this.models.baqsjRecordAccidentOther.otherEventType = this.models.otherEventTypeArr.join(',');
          this.models.baqsjRecordAccidentOther.otherEventType1Type = this.models.otherEventType1TypeArr.join(',');
          this.models.baqsjRecordAccidentOther.otherEventType2Type = this.models.otherEventType2TypeArr.join(',');
          doSaveAccidentOther(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
